package ics.zychen.auth;

import ics.zychen.SnsSelectActivity;
import ics.zychen.Test3Activity;
import ics.zychen.sql.RenrenDataHelper;

import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.renren.api.connect.android.*;
import com.renren.api.connect.android.exception.RenrenAuthError;
import com.renren.api.connect.android.exception.RenrenException;
import com.renren.api.connect.android.users.UserInfo;
import com.renren.api.connect.android.users.UsersGetInfoRequestParam;
import com.renren.api.connect.android.view.RenrenAuthListener;

public class RenrenOAuth extends Activity{
	private Renren renren;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		final String API_KEY = "f79cfacaa4e943349fd23458b2fe3d5d";
		final String SECRET_KEY = "3a640ad285cc4f4fbd5ddc06640537be";
		final String APP_ID = "197463";
		renren = new Renren(API_KEY, SECRET_KEY, APP_ID, this);
		
		renren.authorize(this, listener);
//		this.renren = (Renren)this.getIntent().getBundleExtra("bundle").getParcelable("RENREN");
		
	}

	final RenrenAuthListener listener = new RenrenAuthListener() {

		@Override
		public void onComplete(Bundle values) {
			Log.e("RenrenOAuth", renren.getCurrentUid()+"");
			//startApiList();
			String[] uids = {renren.getCurrentUid()+""};
			UsersGetInfoRequestParam param = new UsersGetInfoRequestParam(uids);
			RenrenDataHelper helper = new RenrenDataHelper(RenrenOAuth.this);
			try {
				ArrayList<UserInfo> users = renren.getUsersInfo(param).getUsersInfo();
				ics.zychen.sql.UserInfo userInfo = new ics.zychen.sql.UserInfo();
				for(UserInfo user : users){
					Log.e("RenrenOAuth", user.getName());
					Log.e("RenrenOAuth", user.getHeadurl());
					userInfo.setUserId(renren.getCurrentUid()+"");
					userInfo.setUserName(user.getName());
					URL iconUrl = new URL(user.getHeadurl());
					Bitmap icon = BitmapFactory.decodeStream(iconUrl.openConnection().getInputStream());
					userInfo.setUserIcon(icon);
					if(helper.HaveUserInfo(userInfo.getUserId()) != null){
						helper.UpdateUserInfo(userInfo);
					} else {
						helper.SaveUserInfo(userInfo);
					}
				}
				
			} catch (RenrenException e) {
				e.printStackTrace();
			} catch (Throwable e) {
				new Exception(e).printStackTrace();
			} finally{
				helper.Close();
				Intent intent = new Intent();
				intent.setClass(RenrenOAuth.this, Test3Activity.class);
				startActivity(intent);
			}
			
		}

		@Override
		public void onRenrenAuthError(
				RenrenAuthError renrenAuthError) {
			
		}

		@Override
		public void onCancelLogin() {
		}

		@Override
		public void onCancelAuth(Bundle values) {
		}
		
	};
	/*
	public void auth(Activity activity){
		renren.authorize(activity, listener);
	}*/
}
